package typingballoon;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;

/**
 *
 * @author Administrator
 */
public class TypingCanvas640 extends GameCanvas implements Runnable {

    //midlet
    private TypingBalloonMIDlet midlet;
    private final int JUM_BALLOON = 26;
    //path
    //private String[] pathBalloon01 = new String[26];
    private String[] pathBalloon01 = new String[JUM_BALLOON];
    private String[] pathQwerty01 = new String[26];
    private String pathQuitOn = "";
    private String pathAttack = "";
    //path level
    //images    
    private Image imgQuitSdOn;
    private Image imgAttack;
    private Image[] imgKey01 = new Image[26];
    //button
    private ButtonImage btnQuit;
    private ButtonImage btnAttack;
    private ButtonImage btnQwerty[] = new ButtonImage[26];
    //font
    public static Font SMALL_PLAIN_FONT;
    public static Font SMALL_BOLD_FONT;
    public static Font LARGE_BOLD_FONT;
    //canvas property
    private int CANVAS_WIDTH;
    private int CANVAS_HEIGHT;
    private int warna;
    //positions
    private int x = 0;
    private int y = 0;
    private int xBg = 0;
    private int yBg = 0;
    private int[] xBalloon = new int[JUM_BALLOON];
    private int[] yBalloon = new int[JUM_BALLOON];
    private int[] xKeypad = new int[26];
    private int[] yKeypad = new int[26];
    private String[] sInfoPosisi = new String[JUM_BALLOON];
    private int xyArea[] = new int[4];
    private int xQuit, yQuit;
    private int xAttack, yAttack;
    //animated sprite
    private AnimatedSprite[] spBalloon01 = new AnimatedSprite[JUM_BALLOON];
    private static int FRAME_SHADOW_WIDTH = 0;
    private static int FRAME_SHADOW_HEIGHT = 0;
    private boolean running = false;
    //private BackgroundScroll bgScroll;
    private LayerManager lmgr;
    //speed 
    private int speedGame = 50;
    private int speedBalloon = 1;
    private int speedGeser = 50;
    private int speedPindahHuruf = 50;
    //huruf
    char key;    
    private String huruf = "0";
    private int iHuruf = 0;
    //random
    private Random myRandom = new Random();
    //boolean value
    private boolean isGameOver = false;
    //score
    private int SCORE = 0;
    private int LEVEL = 0;
    private int LIFE = 0;
    private String sScore = "";
    private String sLive = "";
    private int iLive = 0;
    private String sGameOver = "";
    
    Thread thread = null;
    //disable and paused
    //boolean
    private boolean isDisablePortrait = false;
    private boolean isPaused = false;
    //button
    private ButtonImage btnResume;
    //image
    private Image imgBtnResume;
    private Image imgDisablePortrait;
    private int acuanDisable = 320;
    private String pathBtnResume = "";
    private String pathDisable = "/disableportrait.png";
    
    private Player player = null;

    public TypingCanvas640(TypingBalloonMIDlet midlet) {
        super(false);
        setFullScreenMode(true);

        this.midlet = midlet;

        //CANVAS_WIDTH = getWidth();
        //CANVAS_HEIGHT = getHeight();
        CANVAS_WIDTH = 640;
        CANVAS_HEIGHT = 360;
        //warna = 0xD3F8FF;
        warna = 0xffffff;


        if (CANVAS_WIDTH == 640) {
            FRAME_SHADOW_WIDTH = 94;
            FRAME_SHADOW_HEIGHT = 100;

            for (int i = 0; i < JUM_BALLOON; i++) {
                pathBalloon01[i] = "/balloon01_" + i + "_640.png";
            }

            for (int i = 0; i < 26; i++) {
                pathQwerty01[i] = "/key_1_" + i + ".png";
            }

            pathQuitOn = "/close640.png";

            pathAttack = "/btnBack.png";
        } else {
            FRAME_SHADOW_WIDTH = 47;
            FRAME_SHADOW_HEIGHT = 50;

            for (int i = 0; i < JUM_BALLOON; i++) {
                pathBalloon01[i] = "/balloon01_" + i + "_320.png";
            }

            for (int i = 0; i < 26; i++) {
                pathQwerty01[i] = "/key_1_" + i + ".png";
            }

            pathQuitOn = "/close320.png";
            pathAttack = "/btnBack.png";
        }

        try {
            //bgScroll = new BackgroundScroll(Image.createImage(pathBackground));
            //bgScroll.setRefPixelPosition(xBg, yBg);
            //animated sprite
            lmgr = new LayerManager();

            for (int i = 0; i < JUM_BALLOON; i++) {
                spBalloon01[i] = new AnimatedSprite(Image.createImage(pathBalloon01[i]),
                        FRAME_SHADOW_WIDTH, FRAME_SHADOW_HEIGHT);
                spBalloon01[i].isFired = false;
                spBalloon01[i].defineReferencePixel(FRAME_SHADOW_WIDTH / 2, FRAME_SHADOW_HEIGHT / 2);
                lmgr.append(spBalloon01[i]);
            }

        } catch (IOException ex) {
        }

        try {
            imgAttack = Image.createImage(pathAttack);
            imgQuitSdOn = Image.createImage(pathQuitOn);
            for (int i = 0; i < 26; i++) {
                imgKey01[i] = Image.createImage(pathQwerty01[i]);
            }
        } catch (IOException ex) {
        }

        SMALL_PLAIN_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        SMALL_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL);
        LARGE_BOLD_FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);

        xQuit = 0;
        yQuit = 0;
        xAttack = CANVAS_WIDTH - imgAttack.getWidth();
        yAttack = CANVAS_HEIGHT - imgAttack.getHeight();

        xyArea[0] = yQuit; //x1
        xyArea[1] = yQuit + imgQuitSdOn.getWidth(); //x2
        xyArea[2] = xQuit; //y1
        xyArea[3] = xQuit + imgQuitSdOn.getHeight(); //y2
        btnQuit = new ButtonImage("quit", imgQuitSdOn, imgQuitSdOn,
                imgQuitSdOn, xQuit,
                yQuit, xyArea, Graphics.TOP | Graphics.LEFT);

        xyArea[0] = yAttack; //x1
        xyArea[1] = yAttack + imgAttack.getWidth(); //x2
        xyArea[2] = xAttack; //y1
        xyArea[3] = xAttack + imgAttack.getHeight(); //y2
        btnAttack = new ButtonImage("attack", imgAttack, imgAttack,
                imgAttack, xAttack,
                yAttack, xyArea, Graphics.TOP | Graphics.LEFT);

        for (int i = 0; i < 10; i++) {
            xKeypad[i] = (i * 64) + 2 * (i + 1);
            yKeypad[i] = CANVAS_HEIGHT - 150;

            xyArea[0] = yAttack; //x1
            xyArea[1] = yAttack + imgKey01[i].getWidth(); //x2
            xyArea[2] = xAttack; //y1
            xyArea[3] = xAttack + imgKey01[i].getHeight(); //y2
            btnQwerty[i] = new ButtonImage("KeyPad", imgKey01[i], imgKey01[i],
                    imgKey01[i], xKeypad[i],
                    yKeypad[i], xyArea, Graphics.TOP | Graphics.LEFT);
        }
        for (int i = 10; i < 19; i++) {
            xKeypad[i] = ((i - 10) * 64) + 2 * (i - 10 + 1);
            yKeypad[i] = CANVAS_HEIGHT - 100;

            xyArea[0] = yAttack; //x1
            xyArea[1] = yAttack + imgKey01[i].getWidth(); //x2
            xyArea[2] = xAttack; //y1
            xyArea[3] = xAttack + imgKey01[i].getHeight(); //y2
            btnQwerty[i] = new ButtonImage("KeyPad", imgKey01[i], imgKey01[i],
                    imgKey01[i], xKeypad[i],
                    yKeypad[i], xyArea, Graphics.TOP | Graphics.LEFT);
        }
        for (int i = 19; i < 26; i++) {
            xKeypad[i] = ((i - 19) * 64) + 2 * (i - 19 + 1);
            yKeypad[i] = CANVAS_HEIGHT - 50;

            xyArea[0] = yAttack; //x1
            xyArea[1] = yAttack + imgKey01[i].getWidth(); //x2
            xyArea[2] = xAttack; //y1
            xyArea[3] = xAttack + imgKey01[i].getHeight(); //y2
            btnQwerty[i] = new ButtonImage("KeyPad", imgKey01[i], imgKey01[i],
                    imgKey01[i], xKeypad[i],
                    yKeypad[i], xyArea, Graphics.TOP | Graphics.LEFT);
        }

        for (int i = 0; i < JUM_BALLOON; i++) {
            xBalloon[i] = (getRandomInt((int) (CANVAS_WIDTH * 0.85)));
            yBalloon[i] = (1 + getRandomInt(CANVAS_HEIGHT * 2)) + 150;
        }

        for (int i = 0; i < JUM_BALLOON; i++) {
            spBalloon01[i].setRefPixelPosition(xBalloon[i], yBalloon[i]);
        }

        SCORE = 0;
        LEVEL = 1;
        speedBalloon = 1;
        iLive = 5;

        //System.out.println(CANVAS_WIDTH + "x" + CANVAS_HEIGHT);
        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        }
        updatePositions();
    }
    //start thread
    public void start() {
        if (!isPaused) {
            running = true;
            thread = new Thread(this);
            thread.start();
        }
    }

    //game loop
    public void run() {
        Graphics g = getGraphics();
        while (running) {
            // look for keypress        
            if (!isGameOver) {
                checkForKeys();
                checkGameOver();
                checkScore();
                checkLevel();
            }
            drawDisplay(g);
            //System.out.println(spShuriken.getX());
            try {
                Thread.sleep(speedGame);
            } catch (InterruptedException ie) {
            }
        }
    }

    public void stop() {
        running = false;
    }

    /*
    protected void sizeChanged(int i, int i1) {
    super.sizeChanged(i, i1);
    //System.out.println("Oit!");
    System.out.println(getWidth() + "x" +getHeight());
    }
     *
     */
    private void checkLevel() {
        switch (LEVEL) {
            case 1:
                speedBalloon = 1;
                break;
            case 2:
                speedBalloon = 2;
                break;
            case 3:
                speedBalloon = 3;
                break;
            case 4:
                speedBalloon = 4;
                break;
            case 5:
                speedBalloon = 5;
                break;
            case 6:
                speedBalloon = 6;
                break;
            case 7:
                speedBalloon = 7;
                break;
            case 8:
                speedBalloon = 8;
                break;
            case 9:
                speedBalloon = 9;
                break;
            case 10:
                speedBalloon = 10;
                break;
            case 11:
                speedBalloon = 11;
                break;
            case 12:
                speedBalloon = 12;
                break;
        }
    }

    private void checkScore() {
        switch (SCORE) {
            case 500:
                LEVEL = 2;
                break;
            case 1000:
                LEVEL = 3;
                break;
            case 1500:
                LEVEL = 4;
                break;
            case 2000:
                LEVEL = 5;
                break;
            case 2500:
                LEVEL = 6;
                break;
            case 3000:
                LEVEL = 7;
                break;
            case 3500:
                LEVEL = 8;
                break;
            case 4000:
                LEVEL = 9;
                break;
            case 4500:
                LEVEL = 10;
                break;
            case 5000:
                LEVEL = 11;
                break;
            case 6500:
                LEVEL = 12;
                break;
        }
    }

    private void checkGameOver() {
        if (iLive < 1) {
            LEVEL = 1;
            speedBalloon = 1;
            sGameOver = "GAME OVER!!!!";
            isGameOver = true;
        } else {
            sGameOver = "";
        }
    }

    private void hideBalloon(int i) {
        if (!"GAME OVER!!!!".equals(sGameOver)) {
            if ((yBalloon[i] > 0) && (yBalloon[i] < CANVAS_HEIGHT)) {
                //ditembak
                SCORE = SCORE + 10;
                sScore = String.valueOf(SCORE);
                //midlet.playSoundDuar();
                //playSoundDuar();
                setSoundDuar();
                playSoundDuar();
                spBalloon01[i].setVisible(false);
                //spBalloon01[i].isFired = true;

                //dirandom lagi
                xBalloon[i] = (getRandomInt((int) (CANVAS_WIDTH * 0.85)));
                yBalloon[i] = (1 + getRandomInt(CANVAS_HEIGHT * 2)) + 150;
                if (yBalloon[i] < (CANVAS_HEIGHT + 100)) {
                    yBalloon[i] = CANVAS_HEIGHT + 100;
                }
                spBalloon01[i].setPosition(xBalloon[i], yBalloon[i]);
                spBalloon01[i].setVisible(true);
            } else {
                SCORE = SCORE - 5;
                sScore = String.valueOf(SCORE);
            }
        }
    }

    protected void drawDisplay(Graphics g) {
        g.setColor(warna);
        g.fillRect(0, 0, getWidth(), getHeight());
        sLive = String.valueOf(iLive);
        for (int i = 0; i < JUM_BALLOON; i++) {
            /*
            if (spBalloon01[i].isFired) {
            //spBalloon.nextFrame();
            if (spBalloon01[i].getFrame() == (spBalloon01[i].getFrameSequenceLength() - 1)) {
            //spBalloon.setFrame(0);
            spBalloon01[i].nextFrame();
            spBalloon01[i].setVisible(false);
            spBalloon01[i].isFired = false;
            //System.out.println("oi");            
            } else {
            spBalloon01[i].nextFrame();
            //System.out.println("AH!!");
            }
            }
             * 
             */

            if (yBalloon[i] < -100) {
                if (iLive > 0) {
                    iLive = iLive - 1;

                }
                sLive = String.valueOf(iLive);
                checkGameOver();
                //random aggy
                xBalloon[i] = (getRandomInt((int) (CANVAS_WIDTH * 0.85)));
                yBalloon[i] = (1 + getRandomInt(CANVAS_HEIGHT * 2));
                if (yBalloon[i] < (CANVAS_HEIGHT + 100)) {
                    yBalloon[i] = CANVAS_HEIGHT + 100;
                }
            } else {
                yBalloon[i] = yBalloon[i] - speedBalloon;
            }
            sInfoPosisi[i] = getHuruf(1, i) + " - " + xBalloon[i] + ",   " + yBalloon[i];
            spBalloon01[i].setPosition(xBalloon[i], yBalloon[i]);
        }

        lmgr.paint(g, 0, 0);


        //paint disini
        //sampai sini                

        btnQuit.paint(g);
        //btnAttack.paint(g);
        for (int i = 0; i < 26; i++) {
            btnQwerty[i].paint(g);
        }

        /*
        g.setFont(TypingBalloonMIDlet.SMALL_PLAIN_FONT);
        g.setColor(0xFF0000);
        for (int i = 0; i < JUM_BALLOON; i++) {
        g.drawString(sInfoPosisi[i],
        5, i * 15,
        Graphics.TOP | Graphics.LEFT);
        }
         * 
         */

        g.setColor(0x000000);
        g.setFont(LARGE_BOLD_FONT);

        g.drawString(huruf,
                CANVAS_WIDTH / 2, (int) (CANVAS_HEIGHT * 0.9),
                Graphics.TOP | Graphics.LEFT);

        g.setColor(0x0000FF);
        g.drawString("Life : " + sLive,
                (int) (CANVAS_WIDTH * 0.1), (int) (CANVAS_HEIGHT * 0.2),
                Graphics.TOP | Graphics.LEFT);
        g.drawString("Score : " + sScore,
                (int) (CANVAS_WIDTH * 0.1), (int) (CANVAS_HEIGHT * 0.3),
                Graphics.TOP | Graphics.LEFT);

        g.setColor(0xFF0000);
        g.drawString(sGameOver,
                (int) (CANVAS_WIDTH * 0.25), (int) (CANVAS_HEIGHT * 0.5),
                Graphics.TOP | Graphics.LEFT);
        //flush off-screen buffer to display
        flushGraphics();
    }

    private String getHuruf(int i1, int i2) {
        String s = "";
        switch (i2) {
            case 0:
                s = "A";
                break;
            case 1:
                s = "B";
                break;
            case 2:
                s = "C";
                break;
            case 3:
                s = "D";
                break;
            case 4:
                s = "E";
                break;
            case 5:
                s = "F";
                break;
            case 6:
                s = "G";
                break;
            case 7:
                s = "H";
                break;
            case 8:
                s = "I";
                break;
            case 9:
                s = "J";
                break;
            case 10:
                s = "K";
                break;
            case 11:
                s = "L";
                break;
            case 12:
                s = "M";
                break;
            case 13:
                s = "N";
                break;
            case 14:
                s = "O";
                break;
            case 15:
                s = "P";
                break;
            case 16:
                s = "Q";
                break;
            case 17:
                s = "R";
                break;
            case 18:
                s = "S";
                break;
            case 19:
                s = "T";
                break;
            case 20:
                s = "U";
                break;
            case 21:
                s = "V";
                break;
            case 22:
                s = "W";
                break;
            case 23:
                s = "X";
                break;
            case 24:
                s = "Y";
                break;
            case 25:
                s = "Z";
                break;
            default:
                s = "";
                break;
        }
        return s;
    }

    private void checkForKeys() {
        int keyState = getKeyStates();

        if ((keyState & KEY_NUM3) != 0) {
            //System.out.println("2");
        }

        if ((keyState & FIRE_PRESSED) != 0) {
            //isAttack = true;
            keyPressed(-5);
            //System.out.println(getKeyStates());
        } else if ((keyState & LEFT_PRESSED) != 0) {
            keyPressed(-3);
            //System.out.println(getKeyStates());
        } else if ((keyState & RIGHT_PRESSED) != 0) {
            keyPressed(-4);
            //System.out.println(getKeyStates());
        } else if ((keyState & UP_PRESSED) != 0) {
            keyPressed(-1);
            //System.out.println(getKeyStates());
        } else if ((keyState & DOWN_PRESSED) != 0) {
            keyPressed(-2);
            //System.out.println(getKeyStates());
        } else if ((keyState & GAME_A_PRESSED) != 0) {
            //System.out.println("A!!!");
        } else if ((keyState & GAME_B_PRESSED) != 0) {
            //System.out.println("B!!!");
        } else if ((keyState & GAME_C_PRESSED) != 0) {
            //System.out.println("C!!!");
        } else if ((keyState & GAME_D_PRESSED) != 0) {
            //System.out.println("D!!!");
        }
    }

    protected void keyPressed(int keyCode) {
        //key = String.valueOf(getKeyName(keyCode));

        System.out.print(huruf);
        //key = (char) keyCode;
        //System.out.println(" - char: " + key);
        //qwerty
        if (keyCode == -5) {
        } else if (keyCode == -7) {
        } else if (keyCode == -6) {
            //midlet.showMenuCanvas();
            midlet.exit();
        }
        repaint();

        switch (keyCode) {
            case 0:
                huruf = "Q";
                hideBalloon(16);
                break;
            case 1:
                huruf = "W";
                hideBalloon(22);
                break;
            case 2:
                huruf = "E";
                hideBalloon(4);
                break;
            case 3:
                huruf = "R";
                hideBalloon(17);
                break;
            case 4:
                huruf = "T";
                hideBalloon(19);
                break;
            case 5:
                huruf = "Y";
                hideBalloon(24);
                break;
            case 6:
                huruf = "U";
                hideBalloon(20);
                break;
            case 7:
                huruf = "I";
                hideBalloon(8);
                break;
            case 8:
                huruf = "O";
                hideBalloon(14);
                break;
            case 9:
                huruf = "P";
                hideBalloon(15);
                break;
            case 10:
                huruf = "A";
                hideBalloon(0);
                break;
            case 11:
                huruf = "S";
                hideBalloon(18);
                break;
            case 12:
                huruf = "D";
                hideBalloon(3);
                break;
            case 13:
                huruf = "F";
                hideBalloon(5);
                break;
            case 14:
                huruf = "G";
                hideBalloon(6);
                break;
            case 15:
                huruf = "H";
                hideBalloon(7);
                break;
            case 16:
                huruf = "J";
                hideBalloon(9);
                break;
            case 17:
                huruf = "K";
                hideBalloon(10);
                break;
            case 18:
                huruf = "L";
                hideBalloon(11);
                break;
            case 19:
                huruf = "Z";
                hideBalloon(25);
                break;
            case 20:
                huruf = "X";
                hideBalloon(23);
                break;
            case 21:
                huruf = "C";
                hideBalloon(2);
                break;
            case 22:
                huruf = "V";
                hideBalloon(21);
                break;
            case 23:
                huruf = "B";
                hideBalloon(1);
                break;
            case 24:
                huruf = "N";
                hideBalloon(13);
                break;
            case 25:
                huruf = "M";
                hideBalloon(12);
                break;
            default:
                break;
        }
        repaint();
    }

    protected void pointerPressed(int x, int y) {
        //recognizer = new GestureRecognizer(x, y);
        this.x = x;
        this.y = y;

        if (x > btnQuit.xImage
                && x < (btnQuit.xImage + btnQuit.imageWidth * 2)
                && y > btnQuit.yImage
                && y < (btnQuit.yImage + btnQuit.imageHeight * 2)) {
            keyPressed(-6);
        }

        for (int i = 0; i < 26; i++) {
            if (x > btnQwerty[i].xImage
                    && x < (btnQwerty[i].xImage + btnQwerty[i].imageWidth)
                    && y > btnQwerty[i].yImage
                    && y < (btnQwerty[i].yImage + btnQwerty[i].imageHeight)) {
                keyPressed(i);
            }
        }

        if (x > btnAttack.xImage
                && x < (btnAttack.xImage + btnAttack.imageWidth)
                && y > btnAttack.yImage
                && y < (btnAttack.yImage + btnAttack.imageHeight)) {
            keyPressed(-5);
        }
        repaint();
    }

    private int getRandomInt(int upper) {
        int retVal = myRandom.nextInt() % upper;
        if (retVal < 0) {
            retVal += upper;
        }
        return (retVal);
    }

    private void drawDisablePortrait(Graphics g) {
        g.drawImage(imgDisablePortrait, 0, 0, Graphics.TOP | Graphics.LEFT);
        flushGraphics();
    }
    
    protected void sizeChanged(int w, int h) {
        super.sizeChanged(w, h);
        if (getWidth() < getHeight()) {
            if (getWidth() > acuanDisable) {
                isDisablePortrait = true;
                //System.out.println("PAUSED OY!");
                paused();
            } else {
                isDisablePortrait = false;
            }
        } else {
            isDisablePortrait = false;
            updatePositions();
            Graphics g = getGraphics();
            drawDisplay(g);
            if (isPaused) {
                int xResume = getWidth() - imgBtnResume.getWidth();
                int yResume = getHeight() - imgBtnResume.getHeight();

                btnResume.setPositionXY(xResume, yResume,
                        Graphics.TOP | Graphics.LEFT);
                btnResume.paint(g);
            }
        }
        updatePositions();
    }
    
    private void updatePositions() {
        /*
        xTitle = getWidth() / 2 - (imgTitle.getWidth() / 2);
        yTitle = getHeight() / 2 - (imgTitle.getHeight());
        xString = getWidth() / 2;
        yString = spBalloon.getY() + (spBalloon.getHeight() / 4);
        xShadow = getWidth() / 2 - spBalloon.getWidth();
        yShadow = getHeight() / 2;
        spBalloon.setPosition(xShadow, yShadow);
         * 
         */
    }
    
    protected void hideNotify() {
        super.hideNotify();
        paused();
        int xResume = getWidth() - imgBtnResume.getWidth();
        int yResume = getHeight() - imgBtnResume.getHeight();

        btnResume.setPositionXY(xResume, yResume,
                Graphics.TOP | Graphics.LEFT);
    }

    protected void showNotify() {
        super.showNotify();
        if (!isDisablePortrait) {
            if (isPaused) {
                Graphics g = getGraphics();
                btnResume.paint(g);
                //checkForKeys();
            }
        }
    }

    private void paused() {
        isPaused = true;
        running = false;
        if (isDisablePortrait) {
            Graphics g = getGraphics();
            drawDisablePortrait(g);
        }
    }
    
    private void setSoundDuar() {
        try {
            InputStream in = getClass().getResourceAsStream("/bang.wav");
            player = Manager.createPlayer(in, "audio/x-wav");
            player.setLoopCount(1);   
            
        } catch (Exception e) {
        }
    }

    private void playSoundDuar() {
        try {
            player.stop();
            player.start();
            player.close();
        } catch (MediaException ex) {
        }
    }
    
}
